package Dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import Entity.Course;
import Entity.Event;
import Entity.OpenExp;
import mapper.Coursemapper;
import mapper.Eventmapper;
import mapper.Openexpmapper;
import mapper.Usermapper;

public class openexpdao implements Openexpmapper{
	private  static Openexpmapper openexpmapper;
	private  static SqlSession session;

	public openexpdao() throws IOException {
	 	String resource ="mybatis-config.xml";
		InputStream inputstream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
		 session = sqlsessionfactory.openSession();
        openexpmapper = session.getMapper(Openexpmapper.class);
    }
	public void close(){
		this.session.close();
	}
	@Override
	public boolean CreatOpenExp(OpenExp ope) {
		
		Boolean iuser = this.openexpmapper.CreatOpenExp(ope);
		if(iuser == true){
			session.commit();
		}else{
			session.rollback();
		}
	
		return iuser;
	}

	@Override
	public void deleteOpenExp(String cTitle) {
		
		this.openexpmapper.deleteOpenExp(cTitle);
		session.commit();
	
	}

	@Override
	public List<OpenExp> queryOeByDate(Date date) {
		
		List<OpenExp> selectList = this.openexpmapper.queryOeByDate(date);
		
		return selectList;
	}

	@Override
	public List<OpenExp> queryOe() {
		
		List<OpenExp> selectList = this.openexpmapper.queryOe();
		
		return selectList;
	}

	@Override
	public List<OpenExp> queryOeBypage(int page) {
		Openexpmapper em = null;
		try {
			em = new openexpdao();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<OpenExp> ee = em.queryOe();
		//        从第几条数据开始
        int firstIndex = (page - 1) * 10;
        //	        到第几条数据结束
        int lastIndex = page * 10;
        if(lastIndex<=ee.size())
        	return ee.subList(firstIndex, lastIndex);
        else if(firstIndex>=ee.size())
        	return null;
        else
        	return ee.subList(firstIndex, ee.size());
	}

	@Override
	public List<OpenExp> queryOeByid(String uid) {
			List<OpenExp> selectList = this.openexpmapper.queryOeByid(uid);
	
		return selectList;
	}

	@Override
	public List<OpenExp> queryOeBypageid(String uid,int page) {
		Openexpmapper em = null;
		try {
			em = new openexpdao();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<OpenExp> ee = em.queryOeByid(uid);
		//        从第几条数据开始
        int firstIndex = (page - 1) * 10;
        //	        到第几条数据结束
        int lastIndex = page * 10;
        if(lastIndex<=ee.size())
        	return ee.subList(firstIndex, lastIndex);
        else if(firstIndex>=ee.size())
        	return null;
        else
        	return ee.subList(firstIndex, ee.size());
	}

}
